﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Web.Security;
using Project3.Models;
using Project3.XML;

namespace CodeFirstMembershipSharp
{ 
    public class DataContextInitializer:DropCreateDatabaseAlways<DataContext>
    {
        private DataContext context;
        protected override void Seed(DataContext context)
        {
            this.context = context;
            MembershipCreateStatus Status;

            //NOTE: this will be replaced with logic that reads data from an XML file.

            MembershipConfiguration config = XMLSerializer.DeserializeFromXML();

            foreach (var r in config.Roles) {
                Roles.CreateRole(r);
            }
            context.SaveChanges();

            foreach (var u in config.Users) {
                Membership.CreateUser(u.Username, u.Password, u.Email, null, null, true, out Status);
            }
            context.SaveChanges();

            foreach (var uir in config.UsersInRoles) {
                Roles.AddUserToRole(uir.Username,uir.Role);
            }
            context.SaveChanges();

            var meetings = new List<Meeting>
            {
                new Meeting { Host = GetUserByName("odin"), EventName = "Ragnarok", What = "The end of the world as we know it.", When = DateTime.Parse("01/01/2021 12:00:00 AM"), Where = new Address{Name = "Loki's", Street = "201 Odin's Beard", City = "Reykjavík", State = "Iceland", Zip = 10101},
                    RSVPs = new List<User>()
                        {
                            GetUserByName("abby"),
                            GetUserByName("burt"),
                            GetUserByName("carl"),
                        }
                },
                new Meeting { Host = GetUserByName("olaf"), EventName = "5v5 Classic", What = "20 minutes of romping before the other team surrenders.", When = DateTime.Parse("04/20/2012 4:20:00 PM"), Where = new Address{Name = "Summoner's Rift", Street = "1 Riot Ave", City = "Institute of War", State = "Runeterra", Zip = 10736},
                RSVPs = new List<User>()
                        {
                            GetUserByName("doug"),
                            GetUserByName("eric"),
                            GetUserByName("finn"),
                        }
                },
                new Meeting { Host = GetUserByName("omar"), EventName = "House Screening", What = "2 hour screening of the latest season of House.", When = DateTime.Parse("06/21/2012 9:00:00 AM"), Where = new Address{Name = "Stage 10, 20th Century Fox Studios", Street = "10201 Pico Blvd.", City = "Century City, Los Angeles", State = "California", Zip = 90067},
                RSVPs = new List<User>()
                        {
                            GetUserByName("gabe"),
                            GetUserByName("hugo"),
                            GetUserByName("ivan"),
                        }
                }
            };
            meetings.ForEach(m => context.Meetings.Add(m));

            context.SaveChanges();
        }

        private User GetUserByName(string name)
        {
            return context.Users.SingleOrDefault(q => q.Username == name);
        }
    }
}